﻿#include "GameOverLayer.h"
#include "GameUtil.h"
#include "MainGameScene.h"
#include "StartGameScene.h"

using namespace cocos2d::extension;

GameOverLayer::~GameOverLayer(void)
{
}

bool GameOverLayer::init()
{
	if (!CCLayer::init()) {
		return false;  
	}  
	_winSize = CCDirector::sharedDirector()->getWinSize(); 
	
	m_pUiLayer = UILayer::create();
    addChild(m_pUiLayer);
        
    m_pWidget = dynamic_cast<Layout*>(CCUIHELPER->createWidgetFromJsonFile("MyGame_02_1.json"));
    m_pUiLayer->addWidget(m_pWidget);
	
	// panel
	UIPanel *root = dynamic_cast<UIPanel*>(m_pUiLayer->getWidgetByName("root"));
	root->setPosition(ccp(_winSize.width/2, _winSize.height/2));
	root->setAnchorPoint(ccp(0.5f, 0.5f));

	UITextButton* button_01 = dynamic_cast<UITextButton*>(m_pUiLayer->getWidgetByName("button_01"));
    button_01->addReleaseEvent(this, coco_releaseselector(GameOverLayer::toMainMenu));

    UITextButton* button_02 = dynamic_cast<UITextButton*>(m_pUiLayer->getWidgetByName("button_02"));
    button_02->addReleaseEvent(this, coco_releaseselector(GameOverLayer::restart));

	CocosDenshion::SimpleAudioEngine::sharedEngine()->playBackgroundMusic("sound/gameover.mp3", false);
	return true;  
} 

void GameOverLayer::restart(CCObject* pSender)
{
	CocosDenshion::SimpleAudioEngine::sharedEngine()->end();
	CCDirector::sharedDirector()->replaceScene(MainGameScene::scene());
}

void GameOverLayer::toMainMenu(CCObject* pSender)
{
	CocosDenshion::SimpleAudioEngine::sharedEngine()->end();
	CCDirector::sharedDirector()->replaceScene(StartGameScene::scene());
}

void GameOverLayer::gameOver(CCScene* scene)
{
	CCLayer* gameLayer = (CCLayer *)scene->getChildren()->objectAtIndex(0);
	GameUtil::pause(gameLayer);
	
	scene->addChild(this);

	this->setScale(.0f);
	this->runAction(CCScaleTo::create(1.0f, 1.0f));

}